import request from '@/api/request/axios'

/**
 * 通用接口
 * real:/v1/transaction/real
 * mock:/mock/v1/transaction
 */
export const queryLoss = (params) => request({
  url: window.CONFIG.bizServerUrl + '/v1/transaction/real',
  method: 'post',
  data: params
})

// 发送短信接口
export const sendContentMessage = (params) => request({
  url: window.CONFIG.bizServerUrl + '/v1/message/sendMessage',
  method: 'post',
  data: params
})

// 电核结果通知接口
export const dbResultNotice = (params) => request({
  url: window.CONFIG.dbResultNotice,
  method: 'post',
  data: params
})

/**
 * 交易流水客户信息记录
 * @param cusName 客户姓名
 * @param cardType 证件类型
 * @param cardNo 证件号码
 */
export const saveTransCusInfo = (cusName, cardType, cardNo) => {
  if (!window.Settings.transSwitch) {
    return
  }
  if ('1' == cardType) {
    cardType = '身份证'
  } else if ('2' == cardType) {
    cardType = '户口簿'
  } else if ('3' == cardType) {
    cardType = '军人身份证'
  } else if ('4' == cardType) {
    cardType = '武警身份证'
  } else if ('6' == cardType) {
    cardType = '港澳居民来往内地通行证'
  } else if ('8' == cardType) {
    cardType = '临时居民身份证'
  } else if ('J' == cardType) {
    cardType = '港澳台居民居住证'
  } else if ('T' == cardType) {
    cardType = '台湾居民来往大陆通行证或其他有效旅行证件'
  } else if ('W' == cardType) {
    cardType = '外国人永久居留证'
  }
  let params = {
    agentName: Constants.Agent.agentName || '',
    agentNo: Constants.Agent.agentId || '',
    vqid: (Constants.Model.userData && Constants.Model.userData.vqid) || '',
    sessionId: Constants.Model.sid || '123',
    customerName: cusName,
    idCardType: cardType,
    idCardNo: cardNo
  }
  request({
    url: window.CONFIG.bizServerUrl + '/v1/transaction/recodeCustomerInfo',
    method: 'post',
    data: params
  }).then((res) => {
    console.log('response save trans cus info data is: ' + JSON.stringify(res.data))
  })
}

/**
 * 交易流水记录
 * @param bizCode 交易名称
 * @param status 交易状态
 * @param errMsg 失败原因
 * @param accName 户名
 * @param bankNo 账号
 * @param openBankName 开户行
 */
export const saveTransTraffic = (bizCode, status, errMsg) => {
  let namespace = bizCode.split('-')[0]
  let key = bizCode.split('-')[1]
  let moduleName = ''
  let transName = ''
  if ('Personal' == namespace) {
    moduleName = '个人'
  } else if ('Company' == namespace) {
    moduleName = '公司'
  } else if ('PublicInfo' == namespace) {
    moduleName = '公共信息'
  } else if ('ReportLoss' == namespace) {
    moduleName = '挂失'
  }
  if ('balanceAccountData' == key) {
    transName = '余额查询'
  } else if ('detailSearchData' == key) {
    transName = '明细查询'
  } else if ('telReservationData' == key) {
    transName = '开户行及预留手机号码查询'
  } else if ('loanBusiness' == key) {
    transName = '贷款业务查询'
  } else if ('messageService' == key) {
    transName = '短信银行业务'
  } else if ('passUpdate' == key) {
    transName = '查询密码修改'
  } else if ('wealthCurrentHeld' == key) {
    transName = '理财当前持有'
  } else if ('wealthCurrentTrust' == key) {
    transName = '理财当前委托'
  } else if ('depositInterestRateInquiry' == key) {
    transName = '存款利率查询'
  } else if ('exchangeRateInquiry' == key) {
    transName = '汇率查询'
  } else if ('loanInterestRateInquiry' == key) {
    transName = '贷款利率查询'
  } else if ('wealthProductSupermarket' == key) {
    transName = '理财产品超市'
  } else if ('openBankInquiry' == key) {
    transName = '开户行查询'
  } else if ('fundProductSupermarket' == key) {
    transName = '基金产品超市'
  } else if ('oralLoss' == key) {
    transName = '口头挂失'
  } else if ('permanentLoss' == key) {
    transName = '永久挂失'
  }
  let params = {
    sessionId: Constants.Model.sid || '',
    businessType: moduleName + '-' + transName,
    transactionStatus: status,
    errorMsg: errMsg,
    accountName: sessionStorage.getItem('accName') || '',
    cardNo: sessionStorage.getItem('bankNo') || '',
    openBankName: sessionStorage.getItem('openBankName') || ''
  }
  request({
    url: window.CONFIG.bizServerUrl + '/v1/transaction/recodeTelTraffic',
    method: 'post',
    data: params
  })
}

/**
 * 数据字典翻译
 * @param item 每一项配置
 * @param data 数据对象
 */
export const dataDicTrans = (item, data) => {
  // 有数据字典配置
  if (item.dicData) {
    // 一一对应解析
    if ('select' == item.type) {
      for (let i = 0; i < item.dicData.length; i++) {
        if (data[item.prop] == item.dicData[i].value) {
          return item.dicData[i].label
        }
      }
    }
  } else {
    return data[item.prop]
  }
}
